package com.fenghongzhang.day02;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    private TextView haha;
    private Button click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        haha = (TextView) findViewById(R.id.haha);
        registerForContextMenu(haha);

        click = (Button) findViewById(R.id.click);


    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                haha.setTextColor(Color.rgb(255,0,0));
                break;
            case R.id.greed:
                haha.setTextColor(Color.parseColor("#00ff00"));
                break;
            case R.id.yellow:
                haha.setTextColor(Color.parseColor("#881188"));
                break;
        }
        return super.onContextItemSelected(item);
    }

    public void click(View view) {

        /**
         * 1,上下文
         * 2.显示在指定控件下面
         */
        PopupMenu popupMenu = new PopupMenu(this, haha);
        //加载布局
        popupMenu.inflate(R.menu.menu);
        //点击监听
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                switch (itemId){
                    case R.id.red:
                        haha.setTextColor(Color.rgb(255,0,0));
                        break;
                    case R.id.greed:
                        haha.setTextColor(Color.parseColor("#00ff00"));
                        break;
                    case R.id.yellow:
                        haha.setTextColor(Color.parseColor("#881188"));
                        break;
                }
                return false;
            }
        });
        popupMenu.show();

    }

    public void wind(View view) {
        final PopupWindow popupWindow = new PopupWindow(this);
//        popupWindow.setContentView();
        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);

        TextView viewById = inflate.findViewById(R.id.one);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main2Activity.this, "haha", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        popupWindow.setContentView(inflate);

        popupWindow.setWidth(300);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);


        //修改当前窗口的透明度.
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.5f;
        getWindow().setAttributes(attributes);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1f;
                getWindow().setAttributes(attributes);
            }
        });

        //点击外部可取消
        popupWindow.setOutsideTouchable(true);

        //加入动画
        popupWindow.setAnimationStyle(R.style.pop);

//        popupWindow.showAsDropDown(haha,-100,-100);
        View inflate2 = LayoutInflater.from(this).inflate(R.layout.activity_main2, null);
        popupWindow.showAtLocation(inflate2, Gravity.CENTER,0,0);

    }
}
